package com.itmayiedu.mq;

import com.alibaba.fastjson.JSONObject;
import com.itmayiedu.adapter.MessageAdapter;
import com.itmayiedu.constants.Constants;
import com.itmayiedu.service.EmailService;
import io.netty.handler.codec.json.JsonObjectDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @Author cht 【wonderfor@vip.qq.com】
 * @Date 2018/10/22
 */
@Component
@Slf4j
public class ConsumerDistribute {

    @Autowired
    private EmailService emailService;

    private MessageAdapter messageAdapter;

    /***
     * 监听消息
     */
    @JmsListener(destination = "messages_queue")
    public void distribute(String json) {
        log.info("######消息服务平台接受消息内容:{}######",json);
        if(StringUtils.isEmpty(json)) {
            return ;
        }
        JSONObject rootJson = new JSONObject().parseObject(json);
        JSONObject header = rootJson.getJSONObject("header");
        String interfaceType = header.getString("interfaceType");
        if(StringUtils.isEmpty(interfaceType)) {
            return ;
        }
        //判断接口类型
        if(interfaceType.equals(Constants.MSG_EMAIL)) {
            //调用
            messageAdapter = emailService;
        }

        if(messageAdapter == null) {
            return ;
        }
        JSONObject contentJSON = rootJson.getJSONObject("content");
        messageAdapter.sendMsg(contentJSON);
    }
}
